package com.shuangxi.service;

import com.shuangxi.spring.BeanPostProcessor;
import com.shuangxi.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(String beanName, Object bean) {
        System.out.println(beanName + "前置处理");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(String beanName, Object bean) {
        System.out.println(beanName + "后置处理");
        if ("userService".equals(beanName)) {
            return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), (proxy, method, args) -> {
                System.out.println("后置AOP处理开始");
                return method.invoke(bean, args);
            });
        }
        return bean;
    }
}
